package com.smasher.doc;

import android.content.Context;

import androidx.multidex.MultiDexApplication;

import com.elvishew.xlog.LogConfiguration;
import com.elvishew.xlog.LogLevel;
import com.elvishew.xlog.XLog;
import com.elvishew.xlog.printer.AndroidPrinter;
import com.elvishew.xlog.printer.Printer;
import com.hjq.toast.Toaster;
import com.smasher.core.log.Logger;
import com.smasher.core.other.ApplicationContext;
import com.smasher.core.thread.ThreadPool;
import com.smasher.state.StateViewConfig;
import com.tencent.mmkv.MMKV;
import com.smasher.doc.BuildConfig;

import cat.ereza.customactivityoncrash.config.CaocConfig;

public class DocApplication extends MultiDexApplication {

    boolean isDebug = BuildConfig.DEBUG;
    String applicationId = BuildConfig.APPLICATION_ID;
    int versionCode = BuildConfig.VERSION_CODE;
    String versionName = BuildConfig.VERSION_NAME;


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        ApplicationContext.setApplicationContext(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        ThreadPool.getInstance(ThreadPool.PRIORITY_HIGH).submit(() -> {

            initCrash();

            initMMKV();

            initLog();

            initStateView();
        });
    }


    private void initStateView() {
        StateViewConfig.getInstance().setLoadingColor(R.color.color_EDF3FA);
        StateViewConfig.getInstance().setErrorColor(R.color.color_EDF3FA);
        StateViewConfig.getInstance().setNetErrorColor(R.color.color_EDF3FA);
        StateViewConfig.getInstance().setEmptyColor(R.color.color_EDF3FA);
    }

    private void initMMKV() {
        String rootDir = MMKV.initialize(getApplicationContext());
        System.out.println("mmkv root: " + rootDir);
    }

    private void initCrash() {
//        CrashHandler.getInstance().init(this)

        CaocConfig.Builder.create()
                .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT) //default: CaocConfig.BACKGROUND_MODE_SHOW_CUSTOM
                .enabled(true) //default: true
                .showErrorDetails(true) //default: true
                .showRestartButton(true) //default: true
                .logErrorOnRestart(true) //default: true
                .trackActivities(true) //default: false
                .minTimeBetweenCrashesMs(2000) //default: 3000
//            .errorDrawable(R.drawable.ic_custom_drawable) //default: bug image
//            .restartActivity(YourCustomActivity::class.java) //default: null (your app's launch activity)
//            .errorActivity(YourCustomErrorActivity::class.java) //default: null (default error activity)
//            .eventListener(YourCustomEventListener()) //default: null
//            .customCrashDataCollector(YourCustomCrashDataCollector()) //default: null
                .apply();

        //If you use Firebase Crashlytics or ACRA, please initialize them here as explained above.
    }


    private void initLog() {
        Logger.init(isDebug, "CRM");
        int level = isDebug ? LogLevel.ALL : LogLevel.NONE;
        LogConfiguration.Builder builder = new LogConfiguration.Builder();
        LogConfiguration configuration = builder.logLevel(level).enableBorder().build();
        Printer androidPrinter = new AndroidPrinter(true);
        XLog.init(configuration, androidPrinter);

        // 初始化 Toast 框架
        Toaster.init(this);
    }
}
